package com.zb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.ContactService;
import com.zb.Service.DepartmentService;
import com.zb.Service.FullCalendarService;
import com.zb.Service.SheduleService;
import com.zb.Service.UserService;
import com.zb.entity.Contact;
import com.zb.entity.FullCalendar;
import com.zb.entity.Shedule;
import com.zb.entity.Tree;
import com.zb.entity.User;

@Scope("prototype")
@Component
@RequestMapping("/shedule")
public class SheduleController {
	@Autowired
	private FullCalendarService fcservice;
	@Autowired
	private DepartmentService deserivce;
	@Autowired
	private UserService uservice;
	@Autowired
	private SheduleService ss;
	@Autowired
	private ContactService cs;
	@RequestMapping(params = "op=showcale")
	@ResponseBody
	public List<FullCalendar> show(HttpSession session){
		User user = (User)session.getAttribute("sys_user");
		List<FullCalendar> flist = fcservice.getFullcAboutSelf(user.getId());
		return flist;
	}
	
	@ResponseBody
	@RequestMapping(params = "op=showcombooTree")
	public List getCombooTree(Integer id){
		if(id==null){
			return deserivce.getTree2();
		}else{
			List<User> ulist = uservice.getUserByDeptId(id);
			List<Tree> tlist = new ArrayList<Tree>();
			for (User user : ulist) {
				Tree tree = new Tree();
				tree.setId(user.getId());
				tree.setText(user.getRealname());
				tree.setState("open");
				tlist.add(tree);
			}
			return tlist;
		}
	}
	
	@RequestMapping(params = "op=doadd")
	public String doadd(HttpSession session,Shedule shedule,String cont){
		User u = (User)session.getAttribute("sys_user");
		shedule.setUser(uservice.getUserById(u.getId()));
		ss.addShedule(shedule);
		shedule = ss.getShedule();
		if(cont!=null){
			String[] conts = cont.split(",");
			for (String uid : conts) {
				Integer id = Integer.parseInt(uid);
				Contact con = new Contact();
				con.setShedule(shedule);
				User newu = new User();
				newu.setId(id);
				con.setUser(newu);
				cs.addContact(con);
				uservice.updateSheduleState(1,id);
			}
		}
		return "shedule";
	}
	
	@ResponseBody
	@RequestMapping(params = "op=showone")
	public Shedule getSheduleByid(int id){
		Shedule shed = ss.getSheduleById(id);
		return shed;
	}
	
	@RequestMapping(params = "op=doupdate")
	public String doUpdate(HttpSession session,Shedule shedule,String cont){
		User u = (User)session.getAttribute("sys_user");
		shedule.setUser(u);
		ss.updateShedule(shedule);
		if(cont!=null){
			String[] conts = cont.split(",");
			if(StringUtils.isNumeric(conts[0])&&!conts[0].equals("")){
				cs.deleteContact(shedule.getId());
				for (String uid : conts) {
					Integer id = Integer.parseInt(uid);
					Contact con = new Contact();
					con.setShedule(shedule);
					User newu = new User();
					newu.setId(id);
					con.setUser(newu);
					cs.addContact(con);
				}
			}
		}
		return "shedule";
	}
	
	@RequestMapping(params = "op=dodelete")
	public String doDelete(int id){
		cs.deleteContact(id);
		ss.deleteShedule(id);
		return "shedule";
	}
	
	@RequestMapping(params = "op=showdeptcale")
	@ResponseBody
	public List<FullCalendar> showdept(HttpSession session){
		User user = (User)session.getAttribute("sys_user");
		List<FullCalendar> flist = fcservice.getFullcAboutDept(user.getDepartment().getId(), user.getId());
		return flist;
	}
	@ResponseBody
	@RequestMapping(params = "op=questioninfo")
	public int checkUserInfo(HttpSession session){
		User user = (User)session.getAttribute("sys_user");
		int result = uservice.getSheduleState(user.getId());
		if(result == 0){
			return 0;
		}else{
			uservice.updateSheduleState(0, user.getId());
			return 1;
		}
	}
}
